package com.wechat.wechat.controller;

import com.wechat.wechat.entity.ProductCategory;
import com.wechat.wechat.entity.ProductInfo;
import com.wechat.wechat.exception.SellException;
import com.wechat.wechat.form.ProductForm;
import com.wechat.wechat.service.CategoryService;
import com.wechat.wechat.service.ProductService;
import com.wechat.wechat.utils.KeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author NN
 * @2020/2/26 16:03
 */
@Controller
@RequestMapping("/seller/product")
@Slf4j
public class SellerProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 分页获取订单列表 http://127.0.0.1:8080/sell/seller/product/list
     */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                             @RequestParam(value = "size",defaultValue = "10") Integer size,
                             Map<String,Object> map )
    {
        PageRequest request = new PageRequest(page-1,size);
        Page<ProductInfo> productInfoPage = productService.findAll(request);
        map.put("productInfoPage",productInfoPage);//将数据传给模板文件
        map.put("currentPage",page);
        map.put("size",size);
        return new ModelAndView("product/list",map);

    }

    /**
     * 商品上架 http://127.0.0.1:8080/sell/seller/product/on_sale
     */
    @GetMapping("/on_sale")
    public ModelAndView on_sale(@RequestParam("productId") String productId,
                               Map<String,Object> map)
    {
        try{
            ProductInfo productInfo=productService.onSale(productId);
        }catch(SellException e)
        {
            log.error("【卖家端商品上架】发生异常 e={}",e);
            map.put("msg",e.getMessage()); //返回错误信息
            map.put("url","/sell/seller/product/list"); //3s后跳转的页面
            return new ModelAndView("/common/error",map);//跳转到错误页面
        }
        map.put("url","/sell/seller/product/list");
        return new ModelAndView("/common/success",map);
    }

    /**
     * 商品下架 http://127.0.0.1:8080/sell/seller/product/off_sale
     */
    @GetMapping("/off_sale")
    public ModelAndView off_sale(@RequestParam("productId") String productId,
                                Map<String,Object> map)
    {
        try{
            ProductInfo productInfo=productService.offSale(productId);
        }catch(SellException e)
        {
            log.error("【卖家端商品下架】发生异常 e={}",e);
            map.put("msg",e.getMessage()); //返回错误信息
            map.put("url","/sell/seller/product/list"); //3s后跳转的页面
            return new ModelAndView("/common/error",map);//跳转到错误页面
        }
        map.put("url","/sell/seller/product/list");
        return new ModelAndView("/common/success",map);
    }
    /**
     * 根据商品id查询商品和类目表 http://127.0.0.1:8080/sell/seller/product/index
     */
    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "productId",required = false) String productId,
                              Map<String,Object> map)
    {
        if(!StringUtils.isEmpty(productId))
        {
            ProductInfo productInfo=productService.findOne(productId);
            map.put("productInfo",productInfo);
        }
        //查询所有类目
        List<ProductCategory> categoryList=categoryService.findAll();
        map.put("categoryList",categoryList);
        return new ModelAndView("product/index",map);
    }

    /**
     * 新增、修改商品 http://127.0.0.1:8080/sell/seller/product/save
     */
    @PostMapping("/save")
    //当商品有更新时，将缓存中的商品列表删除
    @CacheEvict(cacheNames = "product",key="productlist")
    public ModelAndView save(@Valid ProductForm form,
                            BindingResult bindingResult,
                             Map<String,Object> map)
    {
        if(bindingResult.hasErrors())
        {
            map.put("msg",bindingResult.getFieldError().getDefaultMessage()); //返回错误信息
            map.put("url","/sell/seller/product/list"); //3s后跳转的页面
            return new ModelAndView("/common/error",map);//跳转到错误页面
        }
        ProductInfo productInfo = new ProductInfo();
        try{
            if(!StringUtils.isEmpty(form.getProductId()))
            {
                productInfo=productService.findOne(form.getProductId());
            }else{
                form.setProductId(KeyUtil.getUniqueKey());
            }
            BeanUtils.copyProperties(form,productInfo);
            productService.save(productInfo);

        }catch(SellException e)
        {
            map.put("msg",e.getMessage()); //返回错误信息
            map.put("url","/sell/seller/product/list"); //3s后跳转的页面
            return new ModelAndView("/common/error",map);//跳转到错误页面
        }
        map.put("url","/sell/seller/product/list");
        return new ModelAndView("/common/success",map);
    }

}
